package com.zyl.Ecs.auth.web.controller;

import com.zyl.Ecs.auth.model.LoginParam;
import com.zyl.Ecs.common.domain.auth.vo.AccountVO;
import com.zyl.Ecs.auth.service.AuthService;
import com.zyl.Ecs.common.response.Result;
import com.zyl.Ecs.common.response.ResultCode;
import com.zyl.Ecs.common.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @version v1.0
 * @ProjectName: Ecs
 * @Package: com.zyl.Ecs.auth.web.controller
 * @ClassName: AuthController
 * @Description TODO (认证控制器)
 * @Author 翟玉龙
 * @Date 2024/12/4-19:31
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/auth")
public class AuthController {

    private final AuthService authService;

    /**
     * 登录
     *
     * @param loginParam 登录参数对象
     * @return {@link Result }<{@link ? }>
     */
    @PostMapping("/login")
    public Result<?> login(
            @RequestBody LoginParam loginParam
    ) {
        String token = authService.login(
                loginParam.getUsername(),
                loginParam.getPassword());
        return Result.success(token)
                .apply(ResultCode.SUCCESS.getMessage());
    }

    /**
     * 通过Token获取用户信息
     *
     * @param token token
     * @return {@link Result }<{@link ? }>
     */
    @GetMapping("/userInfo")
    public Result<?> userInfo(
            @RequestHeader(JwtUtil.AUTH_HEADER_KEY) String token
    ) {
        return Result.success(AccountVO.toVO(authService.userInfo(token)))
                .apply(ResultCode.SUCCESS.getMessage());
    }
}
